package com.atguigu.flink.chapter07.state;

import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/18 14:13
 */
public class Flink02_State_BCState {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        MapStateDescriptor<String, String> controlStateDesc =
            new MapStateDescriptor<>("controlState", String.class, String.class);
        
        DataStreamSource<String> dataStream = env
            .socketTextStream("hadoop162", 8888);
        
        DataStreamSource<String> controlStream = env
            .socketTextStream("hadoop162", 9999);
        
        // 1. 把控制流做成广播流
        BroadcastStream<String> bcStream = controlStream.broadcast(controlStateDesc);
        
        // 2. 数据流进行connect
        dataStream
            .connect(bcStream)
            .process(new BroadcastProcessFunction<String, String, String>() {
                // 处理数据流中的数据
                @Override
                public void processElement(String value,
                                           ReadOnlyContext ctx,
                                           Collector<String> out) throws Exception {
                    ReadOnlyBroadcastState<String, String> bcState = ctx.getBroadcastState(controlStateDesc);
                    String aSwitch = bcState.get("logicSwitch");
                    
                    if ("a".equals(aSwitch)) {
                        out.collect("使用 1 号逻辑处理数据...");
                    } else if ("b".equals(aSwitch)) {
                        out.collect("使用 2 号逻辑处理数据...");
                    } else {
                        out.collect("使用 默认 号逻辑处理数据...");
                    }
                    
                }
                
                // 处理广播流中的数据
                @Override
                public void processBroadcastElement(String value,
                                                    Context ctx,
                                                    Collector<String> out) throws Exception {
                    // 把广播流中的元素放入到广播状态中, 然后数据流中的每个并行度都可以获取到
                    BroadcastState<String, String> bcState = ctx.getBroadcastState(controlStateDesc);
                    bcState.put("logicSwitch", value);
                    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
/*
两个流: 一个流一般叫数据流, 另外一个有些配置信息(控制流)

把控制流做成广播流, 与数据流进行connect, 然后控制流中的数据就在一个广播状态中, 数据流就可以读取到这个广播状态

 */
